define(["jquery","bbcare","bch/app/commSearch/commSearch","as/app/nbna/nbnaDetail.servers","as/app/nbna/nbnaDetail.ui"],function($,$bb,$commSearch,$servers,$ui){
	var nbnaDetail = {};

	/**
	 * 一个能区做完给出提示后的回调函数
	 */
	var callFn = function(numIndex){
        if( numIndex > 0 ){
            $('#tabs > li').eq(numIndex).click();
        }else{
            $("#btn-submit").click();
        }
    };

	/**
	 * 选择答案
	 */
	var onAnswerSelect = function(){
		var data = $(this).data("data");
		if( !data.info.status ){
			$(this).addClass("checked").siblings().removeClass("checked");
	        var id = $(this).attr('quesId'), value = $(this).attr("val");
	        var $tab = $('#tabs > li[class*="checked"]');
	        var index = $tab.index(), idS = JSON.parse( $tab.attr("ids")), name = $tab.attr("name"), end =  $tab.attr("end");
	        var question = data.questions[index].question;
	        data.result[id] = value;
	        if( value == 'N/A' ) $('#tabs > li[class*="checked"]').removeAttr("end");
		    $('#questions > ul[id="'+ id +'"]').attr("score",value);
		    for(var i = 0; i < data.questions[index].question.length; i++){
	            if( data.questions[index].question[i].id == id ) data.questions[index].question[i].score = value;
	        }
	        $servers.countTotalScore(data.result);
	        if( end != "end" ){
	        	if( $servers.isHasNA(data.result,idS) ){
	        		$('#tabs > li[class*="checked"]').addClass("done");
	        		$('#tabs > li[class*="checked"]').attr("end","end");
	                var numIndex = $servers.jumpNext(name,data.questions);
	                var tips = numIndex >= 0 ? "该能区题目已经做完,是否进入下一项？" : "题目已经全部做完，是否提交？";
	                $bb.toastConfirmShow(callFn,numIndex,tips);
	        	}
	        }
        }
	}

	/**
	 * 提交成功后的回调函数
	 */
	var ajaxSubmitGetScoreCallback = function(data){
        $bb.toastShow("提交成功！");
		if( $(".nbnaDetail-wrap").parents(".assess-wrap").length == 0 ){
			$bb.openPage('nbnaHistoryRecord',data);
		}else{
			$.get("page/assess/html/nbna/nbnaHistoryRecord.html",function(html){
				$(".assess-wrap").html(html);
			});
		}
    };

    /**
     * 提交
     */
    var submitFn = function(data){
        var channelId = $bb.getChannelId();
		var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
		var score = $servers.countScore(data.result);
		var postionIds = JSON.stringify({
			"map":data.result
		});
		var postionScores = JSON.stringify({
			"map":score
		});
        var params = {
			"oprInfo":{
				"opCode":"1123",
				"opType":"0",
				"opNote":"【"+ doctorName +"】医生为【"+ data.info.userName +"】患者上传nbna评估数据",
				"channelType":"2",
				"channelId":channelId,
			},
			"evaluationType":data.info.evaluationType,
			"queueUserId":data.info.userId,
			"postionIds":postionIds,
			"postionScores":postionScores,
		};
		if( $(".nbnaDetail-wrap").parents(".assess-wrap").length > 0 ){
			var taskId = $(".nbnaDetail-wrap").parents(".assess-wrap").data("ptTaskId");
			$.extend(params,{"taskId":taskId});
		}
		$servers.ajaxSubmitGetScore(params,ajaxSubmitGetScoreCallback);
    };

	/**
	 * 提交
	 */
	var onSubmit = function(){
		var data = $(this).data("data");
       	if( data.info.status == "01" ){
            ajaxSubmitGetScoreCallback(data.info);
        }else{
        	var notDo = $servers.getNotDoQuestId(data.result);
	        if( notDo.length > 0 ){
	            var tips = "该项评测第" + notDo.join("、") + "题还没有做，确定提交么？";
	            $bb.toastConfirmShow(submitFn,data,tips);
	        }else{
	            submitFn(data);
	        }
        }
	}

	/**
	 * 选择题目
	 */
	var onQuestSelect = function(){
		$(this).addClass("ques-checked").siblings().removeClass("ques-checked");
		var data = $(this).data("data");
		var options = JSON.parse( $(this).attr("options") );
		var score = $(this).attr("score");
        $ui.answer({
        	"id":"quesAnswer",
        	"list":options
        });
        $("#quesAnswer li.options").unbind("click");
        $("#quesAnswer li.options").data("data",data);
		$("#quesAnswer li.options").click(onAnswerSelect);
		$('#quesAnswer li.options[val="'+ score +'"]').addClass("checked");
		// 提交
		$("#btn-submit").unbind("click");
		$("#btn-submit").data("data",data);
		$("#btn-submit").click(onSubmit);
	}

	/**
	 * 选择能区
	 */
	var onTabsSelect = function(){
		$(this).addClass("checked").siblings().removeClass("checked");
		var index = $(this).index();
		var data = $(this).data("data");
		$ui.quest({
			"id":"questions",
			"data":data.questions[index]
		});
		$("#questions .ques-item").unbind("click");
		$("#questions .ques-item").data("data",data);
		$("#questions .ques-item").click(onQuestSelect);
		$("#questions .ques-item").eq(0).click();
	}

	/**
	 * 请求问卷成功后的回调函数
	 */
	var ajaxPostionCallback = function(data,info){
		var result = info.status == "01" ? JSON.parse(info.result).map : {};
		var questions = $servers.handleQuestion(data,result);
		var end;
		if( info.status == "01" ){
		    end = "end";
            $servers.countTotalScore(result);
            $("#btn-submit").html("查看结果");
        }else{
        	result =  $servers.getInitResult(result,questions);
        }
		$ui.tabs({
			"id":"tabs",
			"list":questions,
			"end":end,
		});
		$("#tabs li").unbind("click");
		$("#tabs li").data("data",{"questions":questions,"result":result,"info":info});
		$("#tabs li").click(onTabsSelect);
		$("#tabs li").eq(0).click();
	}

	nbnaDetail.init = function(){
		var params = $bb.getPageParams();
		var info = $servers.handleBaseInfo(params);
		$ui.baseInfo({
			"id":"basicInfo",
			"data":info,
		});
		// $(".nbnaDetail-wrap").parents(".assess-wrap").length 判断是从患者中心打开 还是从nbna菜单打开
		if( $(".nbnaDetail-wrap").parents(".assess-wrap").length > 0 ){
			$("#nbnaInfoWrap").hide();
		}
		// 请求评估问卷
		$.extend(info,{"evaluationType":"nbna"});
		$commSearch.servers.ajaxPostion(info,ajaxPostionCallback);
	}

	return nbnaDetail;
});